package com.Darkra1Zzz.controller;

import com.Darkra1Zzz.entity.PageInfo;
import com.Darkra1Zzz.entity.User;
import com.Darkra1Zzz.service.AdminService;
import com.Darkra1Zzz.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author DarkraiZzz
 * @Time 2021/6/29 18:48
 * @Version 1.0
 */
@Controller
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 通过id查询用户
     */
    @RequestMapping( "findUserById")
    public String findUserById(Integer id, HttpSession session) {
        User user= userService.findUserById(id);
        session.setAttribute("user",user);
        return "useredit";
    }

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "findUser")
    public String findUser(String name, Integer id, String phone,
                                       String email, String vip, Integer pageIndex, Integer pageSize, Model model) {
        User user=new User();
        if (id!=null){
            user.setId(id);
        }
        if (name!=null){
            user.setName(name);
        }
        if (phone!=null){
            user.setPhone(phone);
        }
        if (email!=null){
            user.setEmail(email);
        }
        if (vip!=null){
            user.setVip(vip);
        }
        PageInfo<User> pageInfo = userService.findPageInfo(user,pageIndex,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",user.getName());
        return "userlist";
    }


    /**
     * 修改用户信息
     */
    @RequestMapping(value = "updateUser" ,method = RequestMethod.POST )
    public String updateUser(User user) {
        int s = userService.updateUser(user);
        return "redirect:findUser";
    }


    /**
     * 添加用户信息
     */
    @RequestMapping(value = "addUser" ,method = RequestMethod.POST)
    @ResponseBody
    public String addUser(@RequestBody User user) {
        int s = userService.addUser(user);
        return "userlist";
    }


    /**
     * 删除用户信息
     */
    @RequestMapping( "deleteUser")
    @ResponseBody
    public String deleteUser(Integer id) {
        int s = userService.deleteUser(id);
        return "userlist";
    }


    /**
     * 导出Excel
     */
    @RequestMapping(value = "exportUserList", method = RequestMethod.POST)
    @ResponseBody
    public List<User> exportUser(){
        List<User> userList = userService.getAll();
        return userList;
    }
}
